package com.gxa.uhome.controller;

import com.gxa.uhome.pojo.Admin;
import com.gxa.uhome.pojo.Menu;
import com.gxa.uhome.pojo.Role;
import com.gxa.uhome.pojo.StationMaster;
import com.gxa.uhome.pojo.vo.StationMasterVo;
import com.gxa.uhome.service.LogService;
import com.gxa.uhome.service.StationMasterService;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


/**
 * @author: fanyouzhao
 * @date: 18:44 2021/6/7
 */
@RestController
@CrossOrigin
@RequestMapping("/stationMaster")
@Api(tags = "StationMaster站点员管理模块", produces = "application/json;charset=UTF-8")
public class StationMasterController {
    @Resource
    StationMasterService stationMasterService;
    @Resource
    LogService logService;
    JsonResult jsonResult = null;

    @ApiOperation(value = "添加站点员")
    @PostMapping("/add")
    public JsonResult add(@RequestBody @ApiParam(name = "StationMasterVo站点员对象", value = "传入json格式") StationMasterVo stationMasterVo, HttpSession httpSession) {
        boolean add = stationMasterService.add(stationMasterVo);
        Integer adminId = (Integer) httpSession.getAttribute("AdminId");
        if (add) {
            jsonResult = new JsonResult("1", "添加成功");
            logService.insert("添加站点员", adminId,
                    "添加站点员" + stationMasterVo.getName());
        } else {
            jsonResult = new JsonResult("0", "添加失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "站点员全查询")
    @GetMapping(value = "/selectAll", produces = "application/json;charset=UTF-8")
    public JsonResult selectAll() {
        List<StationMasterVo> stationMasters = stationMasterService.selectAll();
        Integer count = stationMasterService.getCount();
        jsonResult = new JsonResult("1", "查询成功", stationMasters);
        jsonResult.setValue("count", count);
        return jsonResult;
    }

    @ApiOperation(value = "站点员分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码(默认为1)", required = false),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true)
    })
    @GetMapping(value = "/selectByPage", produces = "application/json;charset=UTF-8")
    public JsonResult selectByPage(
            @RequestParam(required = false) Integer page,
            @RequestParam Integer size) {
        List<StationMasterVo> stationMasters = stationMasterService.selectByPage(page, size);
        Integer count = stationMasterService.getCount();
        jsonResult = new JsonResult("1", "查询成功", stationMasters);
        jsonResult.setValue("count", count);
        return jsonResult;
    }

    @ApiOperation(value = "查询某站点的所有站点员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stationName", value = "所属站点名称", required = true),
            @ApiImplicitParam(name = "page", value = "页码(默认为1)", required = false),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true)
    })
    @GetMapping(value = "/selectByStationNameByPage", produces = "application/json;charset=UTF-8")
    public JsonResult selectByStationIdByPage(
            @RequestParam String stationName,
            @RequestParam(required = false) Integer page,
            @RequestParam Integer size) {
        List<StationMasterVo> stationMasters = stationMasterService.selectByStationNameByPage(stationName, page, size);
        Integer countByStationName = stationMasterService.getCountByStationName(stationName);
        jsonResult = new JsonResult("1", "查询成功", stationMasters);
        jsonResult.setValue("count", countByStationName);
        return jsonResult;
    }

    @ApiOperation(value = "站点员登录")
    @PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public JsonResult login(@RequestBody StationMasterVo stationMasterVo, HttpSession httpSession) {
        JsonResult jsonResult = new JsonResult();
        StationMasterVo select = stationMasterService.selectByPhone(stationMasterVo.getPhone());
        if (select != null) {

            StationMasterVo login = stationMasterService.login(stationMasterVo.getPhone(), stationMasterVo.getPwd());
            if (login != null) {
                httpSession.setAttribute("stationMasterPhone", stationMasterVo.getPhone());
                String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcm9tX3VzZXIiOiJCIiwidGFyZ2V0X3VzZXIiOiJBIn0.rSWamyAYwuHCo7IFAgd1oRpSP7nzL7BF5t7ItqpKViM";
                jsonResult = new JsonResult("1", "登录成功");
                jsonResult.setData(login);
                jsonResult.setValue("token", token);
            } else {
                jsonResult = new JsonResult("0", "登录失败，用户名或密码错误");
            }
        } else {
            jsonResult = new JsonResult("0", "登录失败，用户名或密码错误");
        }
        return jsonResult;
    }

    @ApiOperation(value = "站点员退出登录")
    @GetMapping(value = "/logout", produces = "application/json;charset=UTF-8")
    public String logout(HttpSession httpSession) {
        httpSession.removeAttribute("stationMasterPhone");
        return "login";
    }

    @ApiOperation(value = "站点员模糊分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "姓名或电话", required = false),
            @ApiImplicitParam(name = "page", value = "页码(默认为1)", required = false),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true)
    })
    @GetMapping(value = "/selectLikeNameOrPhone", produces = "application/json;charset=UTF-8")
    public JsonResult selectLikeNameOrPhoneByPage(
            @RequestParam(required = false) String content,
            @RequestParam(required = false) Integer page,
            @RequestParam Integer size) {
        List<StationMasterVo> stationMasters = stationMasterService.selectLikeNameOrPhoneByPage(content, page, size);
        Integer countLikeNameOrPhone = stationMasterService.getCountLikeNameOrPhone(content);
        jsonResult = new JsonResult("1", "查询成功", stationMasters);
        jsonResult.setValue("count", countLikeNameOrPhone);
        return jsonResult;
    }

    @ApiOperation(value = "修改站点员信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "stationMasterVo", value = "json格式的站点对象",
                    paramType = "body")
    )
    @PostMapping("/update")
    public JsonResult update(@RequestBody StationMasterVo stationMasterVo, HttpSession httpSession) {
        System.out.println("stationMasterVo = " + stationMasterVo);
        boolean update = stationMasterService.update(stationMasterVo);
        Integer adminId = (Integer) httpSession.getAttribute("AdminId");
        if (update) {
            jsonResult = new JsonResult("1", "修改成功");
            logService.insert("修改站点员信息", adminId,
                    "修改站点员" + stationMasterVo.getName() + "的信息");
        } else {
            jsonResult = new JsonResult("0", "修改失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除站点员")
    @GetMapping(value = "/del", produces = "application/json;charset=UTF-8")
    public JsonResult del(@RequestParam("id") Integer id, HttpSession httpSession) {
        StationMasterVo select = stationMasterService.selectById(id);
        boolean del = stationMasterService.del(id);
        Integer adminId = (Integer) httpSession.getAttribute("AdminId");
        if (del) {
            jsonResult = new JsonResult("1", "删除成功");
            logService.insert("删除站点员", adminId, "删除站点员" + select.getName());
        } else {
            jsonResult = new JsonResult("0", "删除失败");
        }
        return jsonResult;
    }

    @GetMapping("/menu")
    public JsonResult selectAdminAndMenuById(@RequestParam Integer id) {
        JsonResult jsonResult = new JsonResult();
        StationMaster stationMaster = stationMasterService.selectMasterAndMenuById(id);
        List<Menu> pid = new ArrayList<>();
        List<Menu> all = new ArrayList<>();

        for (Role role : stationMaster.getRoleList()) {
            for (Menu menu : role.getMenuList()) {
                all.add(menu);
                if (menu.getPid() == 0) {
                    pid.add(menu);
                }
            }
        }
        for (Menu menu1 : pid) {
            List<Menu> zi = new ArrayList<>();
            for (Menu menu2 : all) {
                if (menu2.getPid().equals(menu1.getId())) {
                    zi.add(menu2);
                }
            }
            menu1.setMenuChildren(zi);
        }
        if (stationMaster != null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("菜单查询成功");
            jsonResult.setData(pid);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("菜单查询失败");
        }
        return jsonResult;
    }
}
